<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="600px"
      :close-on-click-modal="false"
      @close="resetForm('form')"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item
            label="所属章"
            v-if="tableData && tableData.length && form.pid != 0"
          >
            <el-select
              v-model="form.pid"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option
                v-for="item in tableData"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input
              type="textarea"
              :rows="4"
              v-model.trim="form.name"
            ></el-input>
            <p class="txt-explain" v-show="addOrEdit == 1">
              批量添加章（节）时，名称请以“,”隔开，例如：名称,名称,名称
            </p>
          </el-form-item>
          <el-form-item label="排序" prop="orderBy">
            <el-input-number
              v-model="form.orderBy"
              :min="1"
              :max="1000"
              size="medium"
              label="排序"
            ></el-input-number>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  sysCourseChapterAdd,
  sysCourseChapterUpdate,
} from '@/api/admin/courseLibrary/sysCourseChapter.js'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      checkedData: {},
      nodeData: {},
      tableData: [],
      addOrEdit: 1,
      form: {
        pid: '0', //父级节点
        courseId: this.$store.getters.courseId, //课程ID
        type: 1, //类型；1-课程章，2-节
        name: '', //章节名称
        orderBy: '10', //排序
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
      },
    }
  },
  mounted() {
    Bus.$on('showDialogChapterAdd', (v) => {
      this.tableData = v.tableData
      this.nodeData = v.data ? JSON.parse(JSON.stringify(v.data)) : {}
      if (v.type == 'add') {
        this.handleAddOrEdit(1)
      } else {
        this.checkedData = v.data
        this.handleAddOrEdit(2)
      }
    })
  },
  beforeDestroy() {
    Bus.$off('showDialogChapterAdd')
  },
  methods: {
    //显示弹窗
    handleAddOrEdit(type) {
      if (type == 2) {
        this.title = '编辑'
        for (let i in this.checkedData) {
          if (this.form.hasOwnProperty(i)) {
            this.form[i] = this.checkedData[i] || ''
          }
        }
        this.form.id = this.checkedData.id
      } else {
        if (this.nodeData.hasOwnProperty('type')) {
          this.title = '添加节'
          this.form.pid = this.nodeData.id
          this.form.type = 2
        } else {
          this.title = '添加章'
          this.form.pid = 0
          this.form.type = 1
        }
        delete this.form.id
      }
      this.addOrEdit = type
      this.dialogVisible = true
    },
    //提交
    submitForm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.addOrEdit == 1 ? this.add() : this.edit()
        }
      })
    },
    //新增
    add() {
      sysCourseChapterAdd(this.form).then((res) => {
        if (res.data.code == 0) {
          this.dialogVisible = false
          Bus.$emit('refreshListChapterTree')
          this.$message({
            message: '操作成功',
            type: 'success',
          })
        }
      })
    },
    //编辑
    edit() {
      sysCourseChapterUpdate(this.form).then((res) => {
        if (res.data.code == 0) {
          this.dialogVisible = false
          Bus.$emit('refreshListChapterTree')
          this.$message({
            message: '操作成功',
            type: 'success',
          })
        }
      })
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
      for (let key in this.form) {
        if (key != 'courseId') {
          this.form[key] = ''
        }
      }
      this.form.type = 1
      this.form.pid = 0
      this.form.orderBy = 10
    },
  },
}
</script>